<?php

/**
 * \file
 * Classe `RadioChoice`.
 */

namespace pilar\SimpleFormMembers /* \cond */ ; /* \endcond */
{

/**
 * Membre de choix entre plusieurs options.
 *
 * \since 03/02/2009
 * \author Matthieu OVIEDO <matthieu.oviedo@gmail.com>
 */
class RadioChoice extends \pilar\Forms\Member {

protected function loadDefaultParams() {
 parent::loadDefaultParams();
 
 $this->params->define(array(
  'options' => array(),  // Options disponibles $value => $caption.
  'defaultValue' => '', // Valeur par défaut.
 ));
}


protected function initialize() {
 parent::initialize();
 
 $this->joinField(); // Déclaration du champ principal.

 if($this->get('defaultValue') != '') {
  $this->setValue($this->get('defaultValue'));
 }
}


public function guiDomNode($domDoc) {
 $domGui = $domDoc->createElement('gui');

 foreach($this->get('options') as $value => $caption) {
  $domSpan = $domDoc->createElement('span');
  // Caption :
   $domSpan->appendChild(
    $domDoc->createTextNode(' '.$caption.' ')
   );
  // Radio button :
   $domInput = $domDoc->createElement('input');
   $domInput->setAttribute('type', 'radio');
   $domInput->setAttribute('name', $this->globalId());
   $domInput->setAttribute('value', $value);
   $domInput->setAttribute('title', $caption);
   $domSpan->appendChild($domInput);
  $domGui->appendChild($domSpan);
 }
 
 return $domGui;
}


protected function outputGuiController() { ?>
<?php if($this->value() != '') { ?> 
$('input[name="<?php echo $this->globalId() ?>"][value="<?php echo $this->value() ?>"]')[0].checked = true;
<?php } ?>
<?php if($this->status() == 'invalid') { ?>
$('#<?php echo $this->globalId() ?>').addClass('invalid');
<?php } ?>
<?php
}

} // Class.

} // Namespace.

?>
